#!/bin/bash
#
# init script build as PoC for
# 1) normal user (non root)
# 2) rvm support
# 3) pid files in user space
# 4) fedora os

# source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

[ "${NETWORKING}" = "no" ] && exit 0

DAEMON_HOME=/home/kraftvoll/zammad/
#RAILS_ENV=production
RAILS_ENV=test
NAME=zammad
APP_PORT=3000
WS_PORT=6042

ZAMMAD_CONFIG=/etc/sysconfig/zammad
# Read config
[ -f "$ZAMMAD_CONFIG" ] && . "$ZAMMAD_CONFIG"


# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
    # First try to load from a user install
    source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
    # Then try to load from a root install
    source "/usr/local/rvm/scripts/rvm"
else
    printf "ERROR: An RVM installation was not found.\n"
    exit 1
fi

rvm use ruby-2.0.0

RUBY=$(which ruby)
RAILS=$(which rails)
BUNDLE=$(which bundle)

APP_PIDFILE=$DAEMON_HOME/tmp/pids/puma.pid
WS_PIDFILE=$DAEMON_HOME/tmp/pids/websocket.pid
SCHEDULER_PIDFILE=$DAEMON_HOME/tmp/pids/scheduler_runner.pid

start() {

    cd $DAEMON_HOME

    # precompile assets
    if [ "$RAILS_ENV" = "production" ]; then
        echo -n $"Precompile assets for ${NAME}/${RAILS_ENV}"
        $BUNDLE exec rake assets:precompile &> /dev/null && echo_success || echo_failure
        echo
    fi

    # start web server
    echo -n $"Starting ${NAME}/${RAILS_ENV} application server on port: ${APP_PORT}"
#    $RAILS server -d -p $APP_PORT --pid $APP_PIDFILE &> /dev/null
#    thin start --threaded -d -p $APP_PORT --pid $APP_PIDFILE
    puma --pidfile $APP_PIDFILE -d -p $APP_PORT -e $RAILS_ENV &> /dev/null
    sleep 2
    status -p $APP_PIDFILE &> /dev/null && echo_success || echo_failure
    echo

    # start websocket server
    echo -n $"Starting ${NAME}/${RAILS_ENV} web socket server on port: ${WS_PORT}"
    script/websocket-server.rb start -d -p $WS_PORT -i $WS_PIDFILE &> /dev/null &
    sleep 2
    status -p $WS_PIDFILE &> /dev/null && echo_success || echo_failure
    echo

    # start scheduler worker
    echo -n $"Starting ${NAME}/${RAILS_ENV} scheduler worker"
    script/scheduler.rb start &> /dev/null && echo_success || echo_failure
    echo
}

stop() {
    # stop scheduler worker
    echo -n $"Shutting down ${NAME}/${RAILS_ENV} scheduler worker"
    script/scheduler.rb stop &> /dev/null && echo_success || echo_failure
    echo

    # stop websocket server
    echo -n $"Shutting down ${NAME}/${RAILS_ENV} web socket server on port: ${WS_PORT}"
    if test -e $WS_PIDFILE; then
        killproc -p $WS_PIDFILE
    fi
    echo

    # stop web server
    echo -n $"Shutting down ${NAME}/${RAILS_ENV} application server on port: ${APP_PORT}"
    if test -e $APP_PIDFILE; then
        killproc -p $APP_PIDFILE
    fi
    echo

}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    status)
        status -p $APP_PIDFILE "${NAME} ${RAILS_ENV} - application server"
        status -p $WS_PIDFILE "${NAME} ${RAILS_ENV} - web socket server"
    ;;

    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
        exit 3
    ;;
esac

